package com.travel.server.manager.impl.command.article;

import com.travel.common.excel.ExcelData;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.ExcelUtil;
import com.travel.server.manager.dto.request.article.data.ExportArticleDataRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.dto.response.article.data.QuerySingleArticleDataResponse;
import com.travel.server.manager.dto.response.article.data.QuerySingleArticleDataResponse.ArticleDataItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/20 16:23
 * @Desc:
 */
@Component
public class ExportArticleDataCommand extends AbstractCommand<ExportArticleDataRequest, EmptyResponse> {

    @Autowired
    QuerySingleArticleDataCommand command;

    @Override
    protected void checkInput(ExportArticleDataRequest request) {

    }

    @Override
    public EmptyResponse handle(ExportArticleDataRequest request) {
        request.setPage(1);
        request.setLimit(5000);
        QuerySingleArticleDataResponse response = command.handle(request);
        List<ArticleDataItem> list = response.getArticleList();
        if(CollectionUtil.isEmpty(list)){
            throw new WorkException(WorkCode.EXCEL_DATA_NULL);
        }
        List<String> title = Arrays.asList("文章名称", "文章分类", "浏览量", "曝光次数" ,"访客数");
        List<List<Object>> rows = new ArrayList<>();
        for(ArticleDataItem item : list){
            List<Object> objects = new ArrayList<>();
            objects.add(item.getName());
            objects.add(item.getClassName());
            objects.add(item.getBrowser());
            objects.add(item.getExpose());
            objects.add(item.getVisit());
            rows.add(objects);
        }
        String fileName = "单条文章浏览数据";
        ExcelData excelData = new ExcelData(title, rows, fileName);
        ExcelUtil.exportExcel(getHttpServletResponse(), fileName, excelData);
        return new EmptyResponse();
    }
}
